<template>
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <span>筛选搜索</span>
      <div style="margin-top: 15px">
        <el-form
          :inline="true"
          :model="searchCompanyList"
          size="small"
          label-width="140px"
        >
          <el-form-item label="公司名称:">
            <el-input
              style="width: 203px"
              v-model="searchCompanyList.name"
              placeholder="请输入公司名称"
              clearable
            ></el-input>
          </el-form-item>

          <el-form-item label="法定代表人:">
            <el-input
              style="width: 203px"
              v-model="searchCompanyList.legalPerson"
              placeholder="请输入法定代表人"
              clearable
            ></el-input>
          </el-form-item>

          <el-form-item label="服务内容:">
            <el-select
              v-model="searchCompanyList.labelId"
              style="width: 203px"
              placeholder="请选择服务标签"
              clearable
            >
              <el-option
                v-for="(item, index) in TagList"
                :key="index"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <div>
        <el-button
          @click="addCompanyDialogVisible = true"
          type="success"
          style="float: left"
          size="small"
          icon="el-icon-plus"
          >添加</el-button
        >
        <el-button
          @click="handleResetSearch()"
          type="info"
          style="float: right"
          size="small"
          icon="el-icon-delete"
          >重置</el-button
        >
        <el-button
          @click="getList(1)"
          style="float: right; margin-right: 15px; margin-bottom: 15px"
          type="primary"
          size="small"
          icon="el-icon-search"
          >查询</el-button
        >
        <el-dialog
          title="添加公司"
          :visible.sync="addCompanyDialogVisible"
          width="60%"
          :before-close="handleClose"
        >
          <span>
            <el-card class="filter-container" shadow="never">
              <div style="margin-top: 15px">
                <el-form
                  ref="addCompanyMessage"
                  :inline="true"
                  :model="addCompanyMessage"
                  size="small"
                  label-width="140px"
                >
                  <el-form-item label="公司名称:">
                    <el-input
                      style="width: 203px"
                      v-model="addCompanyMessage.name"
                      placeholder="请输入公司名称"
                      clearable
                    ></el-input>
                  </el-form-item>
                  <el-form-item label="营业执照注册号:">
                    <el-input
                      style="width: 203px"
                      v-model="addCompanyMessage.businessLicense"
                      placeholder="请输入营业执照注册号"
                      clearable
                    ></el-input>
                  </el-form-item>
                  <el-form-item label="统一信用代码:">
                    <el-input
                      style="width: 203px"
                      v-model="addCompanyMessage.creditCode"
                      placeholder="请输入统一信用代码"
                      clearable
                    ></el-input>
                  </el-form-item>
                  <el-form-item label="法人:">
                    <el-input
                      style="width: 203px"
                      v-model="addCompanyMessage.legalPerson"
                      placeholder="请输入法人"
                      clearable
                    ></el-input>
                  </el-form-item>
                  <el-form-item label="联系人:">
                    <el-input
                      style="width: 203px"
                      v-model="addCompanyMessage.contact"
                      placeholder="请输入联系人"
                      clearable
                    ></el-input>
                  </el-form-item>
                  <el-form-item label="联系电话:">
                    <el-input
                      style="width: 203px"
                      v-model="addCompanyMessage.phone"
                      placeholder="请输入联系电话"
                      clearable
                    ></el-input>
                  </el-form-item>
                  <!-- 选择省市区 -->
                  <el-form-item label="省份:">
                    <el-select
                      v-model="addCompanyMessage.provinceId"
                      style="width: 203px"
                      placeholder="请选择省份"
                      @change="showCity"
                      clearable
                    >
                      <el-option
                        v-for="(item, index) in provinceOptions"
                        :key="index"
                        :label="item.name"
                        :value="item.id"
                      >
                      </el-option>
                    </el-select>
                  </el-form-item>

                  <el-form-item label="城市:">
                    <el-select
                      v-model="addCompanyMessage.cityId"
                      style="width: 203px"
                      placeholder="请选择城市"
                      @change="showArea"
                      clearable
                    >
                      <el-option
                        v-for="(item, index) in cityOptions"
                        :key="index"
                        :label="item.name"
                        :value="item.id"
                      >
                      </el-option>
                    </el-select>
                  </el-form-item>

                  <el-form-item label="地区:">
                    <el-select
                      v-model="addCompanyMessage.areaId"
                      style="width: 203px"
                      placeholder="请选择地区"
                      clearable
                    >
                      <el-option
                        v-for="(item, index) in areaOptions"
                        :key="index"
                        :label="item.name"
                        :value="item.id"
                      >
                      </el-option>
                    </el-select>
                  </el-form-item>
                  <!-- 详细地址 -->
                  <el-form-item label="详细地址:">
                    <el-input
                      style="width: 203px"
                      v-model="addCompanyMessage.address"
                      placeholder="请输入详细地址"
                      clearable
                    ></el-input>
                  </el-form-item>
                </el-form>
              </div>
            </el-card>
          </span>
          <span slot="footer" class="dialog-footer">
            <el-button @click="addCompanyDialogVisible = false"
              >取 消</el-button
            >
            <el-button type="primary" @click="addCompany()">确 定</el-button>
          </span>
        </el-dialog>
      </div>
    </el-card>

    <div style="margin-top: 15px" class="table-container">
      <el-table
        :data="CompanyList"
        style="width: 100%"
        height="340px"
        border
        v-loading="listLoading"
      >
        <el-table-column
          type="selection"
          width="60"
          align="center"
        ></el-table-column>
        <el-table-column label="公司ID" width="100" align="center">
          <template slot-scope="scope">{{ scope.row.id }}</template>
        </el-table-column>
        <el-table-column label="公司名称" width="250" align="center">
          <template slot-scope="scope">{{ scope.row.name }}</template>
        </el-table-column>
        <el-table-column label="地址" width="250" align="center">
          <template slot-scope="scope">
            <div v-if="scope.row.sysArea">
              <el-tag>{{ scope.row.sysArea.city.province.name }}</el-tag>
              <el-tag type="success">{{ scope.row.sysArea.city.name }}</el-tag>
              <el-tag type="warning">{{ scope.row.sysArea.name }}</el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="标签" width="200" align="center">
          <template slot-scope="scope">
            <el-tag
              v-for="(item, index) in scope.row.labels"
              :key="index"
              closable
              :disable-transitions="false"
              @close="handleCloseTag(scope.row.id, item.id)"
              style="margin-right: 5px"
              effect="plain"
              >{{ item.name }}</el-tag
            >
            <el-select
              v-model="scope.row.selectedTagId"
              filterable
              placeholder="+标签"
              @change="handleSelect(scope.row.id, scope.row.selectedTagId)"
            >
              <el-option
                v-for="item in TagList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </template>
        </el-table-column>
        <el-table-column label="法定代表人" width="150" align="center">
          <template slot-scope="scope">{{ scope.row.legalPerson }}</template>
        </el-table-column>
        <el-table-column label="统一信用代码" width="150" align="center">
          <template slot-scope="scope">{{ scope.row.creditCode }}</template>
        </el-table-column>
        <el-table-column label="营业执照" width="150" align="center">
          <template slot-scope="scope">{{
            scope.row.businessLicense
          }}</template>
        </el-table-column>
        <el-table-column label="联系人" width="150" align="center">
          <template slot-scope="scope">{{ scope.row.contact }}</template>
        </el-table-column>
        <el-table-column label="联系电话" width="150" align="center">
          <template slot-scope="scope">{{ scope.row.phone }}</template>
        </el-table-column>
        <el-table-column label="账号状态" width="134" align="center">
          <template slot-scope="scope">
            <el-tag type="danger" v-if="scope.row.status == 'BAN'">禁用</el-tag>
            <el-tag type="success" v-else-if="scope.row.status == 'NORMAL'"
              >正常</el-tag
            >
            <el-tag type="info" v-else>其他</el-tag></template
          >
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="142" align="center">
          <template slot-scope="scope">
            <el-button
              v-if="scope.row.status == 'NORMAL'"
              @click="handleBan(scope.row.id)"
              size="mini"
              type="warning"
              >禁用</el-button
            >
            <el-button
              v-if="scope.row.status == 'BAN'"
              @click="handleCancleBan(scope.row.id)"
              size="mini"
              type="primary"
              >解禁</el-button
            >
            <el-button
              @click="handleDelete(scope.row.id)"
              size="mini"
              type="danger"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="block">
      <el-pagination layout="prev, pager, next" :page-count="pageCunt" @current-change="getList">
      </el-pagination>
    </div>
  </div>
</template>
<script>
export default {
  name: "companyManage",
  data() {
    return {
      searchCompanyList: {
        name: "",
        legalpeople: "",
        labels: "",
        pageNum: "1",
        pageSize: "5",
      },
      currentCompanyList: {
        name: "",
        legalPerson: "",
        creditCode: "",
        businessLicense: "",
        contact: "",
        phone: "",
        areaId: "",
        cityId: "",
        provinceId: "",
        labelId: "",
        status: "",
        pageNum: "1",
        pageSize: "5",
      },
      CompanyList: [],
      addCompanyMessage: {
        businessLicense: "",
        contact: "",
        creditCode: "",
        legalPerson: "",
        name: "",
        phone: "",
        provinceId:"",
        cityId:"",
        areaId:"",
        address:"",
      },
      currentTagList: {
        id: "",
        name: "",
        status: "NORMAL",
      },
      TagList: [],
      pageCunt: 1,
      currentPage: 1,
      value: true,
      listLoading: true,
      addCompanyDialogVisible: false,
      // labelEditDialogVisible: false,
      // inputVisible: false,
      // inputValue: "",
      selectedTagId: "",
      provinceOptions: [],
      cityOptions: [],
      areaOptions: [],
    };
  },
  created() {
    this.getAreaList();
    this.getTagList();
    this.getProvince();
  },
  methods: {
    async getList(pageNum) {
      this.currentCompanyList = this.searchCompanyList
      this.currentCompanyList.pageNum = pageNum
      this.listLoading = true;
      this.axios
        .get("/api/sys/company/list", {
          params: this.currentCompanyList,
        })
        .then((response) => {
          if (response.data.code == "200") {
            this.CompanyList = response.data.value.data;
            // for (var key1 in this.CompanyList) {
            //   for (var key2 in this.AreaList) {
            //     if (this.CompanyList[key1].areaId == this.AreaList[key2].id) {
            //       this.CompanyList[key1].area = this.AreaList[key2].name;
            //       this.CompanyList[key1].city = this.AreaList[key2].city.name;
            //       this.CompanyList[key1].province =
            //         this.AreaList[key2].city.province.name;
            //     }
            //   }
            // }
            if (
              response.data.value.total / 5 >
              Number.parseInt(response.data.value.total / 5)
            ) {
              this.pageCunt =
                Number.parseInt(response.data.value.total / 5) + 1;
            } else {
              this.pageCunt = Number.parseInt(response.data.value.total / 5);
            }
            this.currentPage = 1;
          } else {
            this.$message.error(response.data.message);
          }
          this.listLoading = false;
        })
        .catch((error) => {
          this.$message.error("无法获取数据");
          this.listLoading = false;
        });
    },
    getTagList() {
      this.axios
        .get("/api/sys/label/list", {
          params: this.currentTagList,
        })
        .then((response) => {
          if (response.data.code == "200") {
            this.TagList = response.data.value;
          } else {
            this.$message.error(response.data.message);
          }
        })
        .catch((error) => {
          this.$message.error("无法获取数据");
        });
    },

    getProvince(){
      this.axios
        .get("/api/sys/listProvince")
        .then((response) => {
          if (response.data.code == "200") {
            // console.log(response.data.value);
            this.provinceOptions = response.data.value;
          } else {
            this.$message.error(response.data.message);
          }
        })
        .catch((error) => {
          this.$message.error("无法获取数据");
        });
    },
    showCity(item) {
      this.addCompanyMessage.areaId = "";
      this.addCompanyMessage.cityId = "";
      this.cityOptions = [];
      this.areaOptions = [];
      if (item != 0) {
        this.axios
          .get("/api/sys/listCity?provinceId=" + item)
          .then((response) => {
            this.cityOptions = response.data.value;
          })
          .catch((error) => {});
      }
    },
     showArea(item) {
      this.addCompanyMessage.areaId = "";
      this.areaOptions = [];
      if (item != 0) {
        this.axios
          .get("/api/sys/listArea?cityId=" + item)
          .then((response) => {
            this.areaOptions = response.data.value;
          })
          .catch((error) => {});
      }
    },
   
    async getAreaList() {
      this.axios
        .get("/api/sys/listArea", {
          params: this.currentAreaList,
        })
        .then((response) => {
          if (response.data.code == "200") {
            this.AreaList = response.data.value;
            this.getList(1);
          } else {
            this.$message.error(response.data.message);
          }
        })
        .catch((error) => {
          this.$message.error("无法获取数据");
        });
    },
    addCompany() {
      this.loading = true;
      this.$refs.addCompanyMessage.validate((valid) => {
        if (valid) {
            this.axios
              .post(
                "api/sys/auth/company/add?businessLicense=" +
                  this.addCompanyMessage.businessLicense +
                  "&contact=" +
                  this.addCompanyMessage.contact +
                  "&creditCode=" +
                  this.addCompanyMessage.creditCode +
                  "&legalPerson=" +
                  this.addCompanyMessage.legalPerson +
                  "&name=" +
                  this.addCompanyMessage.name +
                  "&phone=" +
                  this.addCompanyMessage.phone +
                  "&areaId=" +
                  this.addCompanyMessage.areaId +
                  "&address=" +
                  this.addCompanyMessage.address
              )
              .then((response) => {
                console.log(response);
                if (response.data.code == 200) {
                  this.$message({
                    message: "添加成功",
                    type: "success",
                  });
                  this.getList(1);
                  this.addCompanyDialogVisible=false;
                } else {
                  this.$message.error(response.data.message);
                }
              })
              .catch((error) => {
                this.$message.error("添加失败");
              });
        } else {
          this.$message.error("请填写全部内容");
          return false;
        }
      });
      this.loading = false;
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
    handleResetSearch() {
      this.searchCompanyList = {
        name: "",
        credit: "",
        legalpeople: "",
        service: "",
        pageNum: "1",
        pageSize: "5",
      };
    },
    handleSearchList() {
      this.listLoading = true;
      this.axios
        .get("/api/sys/company/list", {
          params: this.searchCompanyList,
        })
        .then((response) => {
          if (response.data.code == "200") {
            this.CompanyList = response.data.value.data;
            if (
              response.data.value.total / 5 >
              Number.parseInt(response.data.value.total / 5)
            ) {
              this.pageCunt =
                Number.parseInt(response.data.value.total / 5) + 1;
            } else {
              this.pageCunt = Number.parseInt(response.data.value.total / 5);
            }
            this.currentPage = 1;
          } else {
            this.$message.error(response.data.message);
          }
          this.listLoading = false;
        })
        .catch((error) => {
          this.$message.error("无法获取数据");
          this.listLoading = false;
        });
    },
    handleBan(banId) {
      if (banId == "") {
        this.$message.error("尚未选择公司，请正确选择后再禁用");
      } else {
        this.$confirm("请再次确认是否禁用该公司")
          .then((_) => {
            this.axios
              .post(
                "api/sys/auth/company/update?id=" + banId + "&status=" + "BAN"
              )
              .then((response) => {
                if (response.data.code == "200") {
                  this.$message({
                    message: "禁用成功",
                    type: "success",
                  });
                  this.getList(1);
                } else {
                  this.$message.error(response.data.message);
                }
              })
              .catch((error) => {
                this.$message.error("禁用失败");
              });
          })
          .catch((_) => {});
      }
    },
    handleCancleBan(cancleBanId) {
      if (cancleBanId == "") {
        this.$message.error("尚未选择公司，请正确选择后再解除禁用");
      } else {
        this.$confirm("请再次确认是否解除禁用该公司")
          .then((_) => {
            this.axios
              .post(
                "api/sys/auth/company/update?id=" +
                  cancleBanId +
                  "&status=" +
                  "NORMAL"
              )
              .then((response) => {
                if (response.data.code == "200") {
                  this.$message({
                    message: "解除禁用成功",
                    type: "success",
                  });
                  this.getList(1);
                } else {
                  this.$message.error(response.data.message);
                }
              })
              .catch((error) => {
                this.$message.error("解除禁用失败");
              });
          })
          .catch((_) => {});
      }
    },
    handleDelete(deleteId) {
      if (deleteId == "") {
        this.$message.error("尚未选择公司，请正确选择后再删除");
      } else {
        this.$confirm("请再次确认是否删除该公司")
          .then((_) => {
            this.axios
              .post(
                "api/sys/auth/company/update?id=" +
                  deleteId +
                  "&status=" +
                  "DELETE"
              )
              .then((response) => {
                if (response.data.code == "200") {
                  this.$message({
                    message: "删除成功",
                    type: "success",
                  });
                  this.getList(1);
                } else {
                  this.$message.error(response.data.message);
                }
              })
              .catch((error) => {
                this.$message.error("删除失败");
              });
          })
          .catch((_) => {});
      }
    },
    handleCloseTag(deleteId, tagId) {
      this.$confirm("请再次确认是否删除该标签")
        .then((_) => {
          this.axios
            .post(
              "api/sys/auth/company/label/remove?companyId=" +
                deleteId +
                "&labelId=" +
                tagId
            )
            .then((response) => {
              if (response.data.code == "200") {
                this.$message({
                  message: "删除成功",
                  type: "success",
                });
                this.getList(1);
              } else {
                this.$message.error(response.data.message);
              }
            })
            .catch((error) => {
              this.$message.error("删除失败");
            });
        })
        .catch((_) => {});
    },
    handleSelect(addId, tagId) {
      this.$confirm("请再次确认是否添加该标签")
        .then((_) => {
          this.axios
            .post(
              "api/sys/auth/company/label/add?companyId=" +
                addId +
                "&labelId=" +
                tagId
            )
            .then((response) => {
              if (response.data.code == "200") {
                this.$message({
                  message: "添加成功",
                  type: "success",
                });
                this.getList(1);
              } else {
                this.$message.error(response.data.message);
              }
            })
            .catch((error) => {
              this.$message.error("添加失败");
            });
        })
        .catch((_) => {});
    },
  },
};
</script>
<style>
.el-tag + .el-tag {
  margin-left: 10px;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
</style>